﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;

public partial class AddOprRecord : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");

        if (!Page.IsPostBack)
        {
            if (Request.QueryString["work_id"] != null)
            {
                int workId = int.Parse(Request.QueryString["work_id"]);
                DAL.WorkDALs workdals = new DAL.WorkDALs();
                Work work = workdals.GetByID(workId);

                DAL.FlowDALs flow = new DAL.FlowDALs();
                this.DropFlowName.DataSource = flow.GetList(null, null);
                this.DropFlowName.DataBind();

                this.DropFlowName.Items[this.DropFlowName.SelectedIndex].Selected = false;

                foreach (ListItem it in this.DropFlowName.Items)
                {
                    if (it.Value == work.Flow_id.ToString())
                    {
                        it.Selected = true;
                        break;
                    }
                }
                
                this.DropOprType.Items[this.DropOprType.SelectedIndex].Selected = false;
                this.OprResultType.Items[this.OprResultType.SelectedIndex].Selected = false;
     
                if (work.Result_type.Trim() == "0")
                    this.OprResultType.Items[0].Selected = true;
                else if (work.Result_type.Trim() == "1")
                     this.OprResultType.Items[1].Selected = true;
                 else if (work.Result_type.Trim() == "2")
                    this.OprResultType.Items[2].Selected = true;
                else if (work.Result_type.Trim() == "3")
                    this.OprResultType.Items[3].Selected = true;
                else if (work.Result_type.Trim() == "4")
                    this.OprResultType.Items[4].Selected = true;

                if (work.Process_type == 0)
                    this.DropOprType.Items[0].Selected = true;
                else if (work.Process_type == 1)
                    this.DropOprType.Items[1].Selected = true;
                else if (work.Process_type == 2)
                    this.DropOprType.Items[2].Selected = true;



                TxtStartTime.value = work.Start_time.Date.ToShortDateString() + " " + work.Start_time.TimeOfDay.ToString();
                TxtEndTime.value = work.End_time.ToShortDateString() + " " + work.End_time.TimeOfDay.ToString();
                this.TxtTagVersion.Text = work.Tag_name;
                this.ProductName.Text = work.Production_name;
                
                this.updatefilelist.Text = work.Files;
                this.TxtFaultDesc.Text = work.Process_description;
                this.FaultDisposedMethod.Text = work.Fault;
                this.PM.Text= work.Pm;
                this.PA.Text = work.Pd;
                this.DEV.Text = work.Dev;
                this.QA.Text = work.Qa;
                this.Marcket.Text = work.Market;

                DAL.Work_userDALs work_userdals = new DAL.Work_userDALs();
                System.Collections.Generic.IList<Work_user> list1 = work_userdals.GetByWorkId(work.Work_id);
                System.Collections.Generic.IList<User> lst = new System.Collections.Generic.List<User>();

                DAL.UserDALs userdals = new DAL.UserDALs();
                this.ChkOprUser.DataSource = userdals.GetList();
                this.ChkOprUser.DataBind();


                foreach (Work_user work_user in list1)
                {
                    int id = work_user.User_id;

                    foreach (ListItem item in this.ChkOprUser.Items)
                    {
                        if (item.Value == id.ToString())
                        {
                            item.Selected = true;
                        }
                    } 

                }

                

                this.Button1.Text = "修改";

                this.workid.Value = work.Work_id.ToString();
                this.signType.Value = "1";
                Session.Remove("work");
            }
            else
            {
                DAL.FlowDALs flow = new DAL.FlowDALs();
                this.DropFlowName.DataSource = flow.GetList(null, null);
                this.DropFlowName.DataBind();

                DAL.UserDALs userdals = new DAL.UserDALs();
                this.ChkOprUser.DataSource = userdals.GetList();
                this.ChkOprUser.DataBind();
            }
            
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(this.signType.Value != "1") 
        {
            //string str = this.TxtStartTime1.value;
            string username = Session["AdminName"].ToString();
            DAL.UserDALs userdals = new DAL.UserDALs();
            User user = userdals.GetByName(username);

            DAL.WorkDALs workdals = new DAL.WorkDALs();
            int flow_id = int.Parse(this.DropFlowName.SelectedValue);
            DateTime start_time = System.DateTime.Parse(TxtStartTime.value);
            DateTime end_time = System.DateTime.Parse(this.TxtEndTime.value);
            string Tag_name = this.TxtTagVersion.Text;
            string production_name = this.ProductName.Text;
            int process_type = int.Parse(this.DropOprType.SelectedValue);
            int result_type = int.Parse(this.OprResultType.SelectedValue);
            string files = this.updatefilelist.Text;
            string process_description = TxtFaultDesc.Text;
            string fault = this.FaultDisposedMethod.Text;
            string pm = this.PM.Text;
            string pd = this.PA.Text;
            string dev = this.DEV.Text;
            string qa = this.QA.Text;
            string market = this.Marcket.Text;
            
            int work_id = workdals.Add(flow_id, start_time, end_time, production_name, Tag_name, process_type, result_type,
                files, process_description, fault, pm, pd, dev, qa, market, user.User_name);


            DAL.Work_userDALs ww = new DAL.Work_userDALs();
            foreach (ListItem item in this.ChkOprUser.Items)
            {
                if (item.Selected)
                {
                    ww.Add(int.Parse(item.Value),work_id);
                }
            }

            Response.Redirect("oprrecord.aspx");
           
        }
        else 
        {
            //更新
            DAL.WorkDALs workdals = new DAL.WorkDALs();
            Work work = workdals.GetByID(int.Parse(this.workid.Value));

            work.Flow_id = int.Parse(this.DropFlowName.SelectedValue);
            work.Start_time = System.DateTime.Parse(TxtStartTime.value);
            work.End_time = System.DateTime.Parse(TxtEndTime.value);
            work.Tag_name = this.TxtTagVersion.Text;
            work.Production_name = this.ProductName.Text;
            work.Process_type = short.Parse(this.DropOprType.SelectedValue);
            work.Result_type = this.OprResultType.SelectedValue;
            work.Files = this.updatefilelist.Text;
            work.Process_description = TxtFaultDesc.Text;
            work.Fault = this.FaultDisposedMethod.Text;
            work.Pm = this.PM.Text;
            work.Pd = this.PA.Text;
            work.Dev = this.DEV.Text;
            work.Qa = this.QA.Text;
            work.Market = this.Marcket.Text;

            workdals.CreateUpdateDeleteWork(work, DAL.DataProviderAction.Update);


            DAL.Work_userDALs work_userdals = new DAL.Work_userDALs();
            System.Collections.Generic.IList<Work_user> list1 = work_userdals.GetByWorkId(work.Work_id);
            System.Collections.Generic.IList<String> lst = new System.Collections.Generic.List<String>();

            foreach (Work_user work_user in list1)
            {
                work_userdals.DeleteWork_user(work_user.Id);
            }

            foreach (ListItem item in this.ChkOprUser.Items)
            {

                if (item.Selected == true)
                {
                    lst.Add(item.Value);
                }
            }

    
            foreach (string str in lst)
            {
                work_userdals.Add(int.Parse(str), work.Work_id);
            }

            Response.Redirect("oprrecord.aspx");
        }
    }
}
